<?php

class Emms_Class_Loan_PmtPlanFlat Extends Emms_Class_Loan_PmtPlan
{
    protected function _setPlan()
    {       
        $this->_plan->pmt = $this->_pmt;
        
        $this->_plan->kp = $this->_principal;
        $this->_plan->kh = 0.01 * $this->_principal * $this->_healthInsurancePct;
        $this->_plan->kl = 0.01 * $this->_principal * $this->_loanInsurancePct;
        $this->_plan->ka = 0.01 * $this->_principal * $this->_administrativeFeePct;
        $this->_plan->ki = ($this->_pmt * $this->_installments) - ($this->_plan->kp + $this->_plan->kh + $this->_plan->kl + $this->_plan->ka);
        
        $this->_plan->kt = $this->_plan->kp + $this->_plan->kh + $this->_plan->kl + $this->_plan->ka + $this->_plan->ki;
        
        $this->_plan->kpP = $this->_plan->kp / $this->_plan->kt;
        $this->_plan->khP = $this->_plan->kh / $this->_plan->kt;
        $this->_plan->klP = $this->_plan->kl / $this->_plan->kt;
        $this->_plan->kaP = $this->_plan->ka / $this->_plan->kt;
        $this->_plan->kiP = $this->_plan->ki / $this->_plan->kt;
        
        $this->_plan->balanceKp = $this->_plan->kp;
        $this->_plan->balanceKh = $this->_plan->kh;
        $this->_plan->balanceKl = $this->_plan->kl;
        $this->_plan->balanceKa = $this->_plan->ka;
        $this->_plan->balanceKi = $this->_plan->ki;
        
        //$this->_plan->annualEffectiveRate;
                
        $this->_plan->xpPmtDate = $this->_firstPaymentDate;
        $this->_plan->xpPmt = $this->_plan->pmt;
        $this->_plan->rFromDate = $this->_principalGraceEndDate;
        
        $this->_plan->schedule = $this->_getSchedule();
    }
    
    protected function _nextPaymentData()
    {       
        $data = array();        
        $balance = $this->_plan->balanceKp + $this->_plan->balanceKh + $this->_plan->balanceKl + $this->_plan->balanceKa + $this->_plan->balanceKi;

        $data['xpPmtDate'] = $this->_nextPaymentDate();
        $data['rFromDate'] = $this->_plan->xpPmtDate;
        
        $data['principal'] = round($this->_plan->kpP * $this->_plan->pmt,2);
        $data['hFee']      = round($this->_plan->khP * $this->_plan->pmt,2);
        $data['lFee']      = round($this->_plan->klP * $this->_plan->pmt,2);
        $data['aFee']      = round($this->_plan->kaP * $this->_plan->pmt,2);
        $data['interest']  = round($this->_plan->kiP * $this->_plan->pmt,2);
        
        $data['balanceKp'] = $this->_plan->balanceKp - $data['principal'];
        $data['balanceKh'] = $this->_plan->balanceKh - $data['hFee'];
        $data['balanceKl'] = $this->_plan->balanceKl - $data['lFee'];
        $data['balanceKa'] = $this->_plan->balanceKa - $data['aFee'];
        $data['balanceKi'] = $this->_plan->balanceKi - $data['interest'];
        
        $data['pmt']       = $this->_plan->pmt; // + penalties
        $data['xpPmt']     = min($this->_plan->pmt, $balance);
        /*
        $this->load_penalties();
        $data['delay']       = $this->data['delay'];
        $data['penalties']   = $this->data['penalties'];
        $data['pmt']         = $this->data['pmt'] + $this->data['penalties'];
        $data['xp_pmt']      = min($this->data['pmt'],$balance + $data['interest']);
         * 
         */
        
        if ($data['balanceKp'] < $data['principal']) {
          $data['principal'] = $this->_plan->balanceKp;
          $data['hFee']      = $this->_plan->balanceKh;
          $data['lFee']      = $this->_plan->balanceKl;
          $data['aFee']      = $this->_plan->balanceKa;
          $data['interest']  = round($this->_plan->kiP * ($this->_plan->balanceKp/$this->_plan->kpP), 2);
          $data['balanceKp'] = 0;
          $data['balanceKh'] = 0;
          $data['balanceKl'] = 0;
          $data['balanceKa'] = 0;
          $data['pmt']       = $data['interest'] + $data['hFee'] + $data['lFee'] + $data['aFee'] + $data['principal']; // + $data['penalties'];
          $data['xpPmt']     = 0;
        }
        return $data;
    }

    protected function _setPmt()
    {
        $i = (($this->_annualPercentageRate/100)*$this->_term)/($this->_calendarType);
        $kpTotal = $this->_principal * ((100 + $this->_loanInsurancePct + $this->_healthInsurancePct + $this->_administrativeFeePct)/100);        
        $this->_pmt = round(($kpTotal*(1 + $i)/$this->_installments),2);
        return $this;
    }
}
?>
